'use strict'

class login{
    constructor(){
        this.data={
            appid:'wxa801bfb7a2c01925',
            secret:'4d3b7a0c5126dce2219b1c773ae52ed5'
        }
    }
    async list(c){
        const gohttp=require('gohttp')
        // console.log(c.query)
        let token_key=c.service.tokenKey
        let login_url=`https://api.weixin.qq.com/sns/jscode2session?appid=${this.data.appid}&secret=${this.data.secret}&js_code=${c.query.code}&grant_type=authorization_code`
        let result=await gohttp.get(login_url)
        let r=result.json()
        if(r.openid===undefined){
            c.status(500)
            return
        }
        let info={
            openid:r.openid,
            expires:7200000,
            timestamp:Date.now(),
            random:Math.random()
        }
        let token=c.helper.aesEncrypt(JSON.stringify(info),token_key)
        // console.log(r.openid)
        c.res.body={
            token,
            userid:r.openid
        }
    }
}
module.exports=login